#ifndef __EVENT_H__
#define __EVENT_H__

#include "linux_def.h"
#include "core.h"


typedef struct {
	int	lock;
	event_t	*events;
	event_t	*last;
}event_mutex_t;

struct event_s{
	void	*data;

	unsigned	write:1;
	unsigned	accept:1;
	unsigned	instance:1;
	unsigned	active:1;
	unsigned	ready:1;
	unsigned	timedout:1;
	unsigned	complete:1;
	unsigned	delay:1;
	unsigned	timer_set:1;
	unsigned	closed:1;
	
	event_handler_pt	handler;
	log_t		*log;

	int	index;
	rbtree_node_t	timer;
	
	event_t		*next;
	event_t		**prev;	
};

#define LEVEL_EVENT	0
#define CLOSE_EVENT	1
#define DISABLE_EVENT	2
#define READ_EVENT	EPOLLIN
#define WRITE_EVENT	EPOLLOUT
#define CLEAR_EVENT	EPOLLET

#define USE_CLEAR_EVENT		0x000000004
#define USE_TIMER_EVENT		0x000000800
#define USE_LEVEL_EVENT		0x000000001
#define USE_GREEDY_EVENT	0x000000002
#define USE_EPOLL_EVENT		0x000000004

#define UPDATE_TIME	1
#define POST_EVENTS	2
#define POST_THREAD_EVENTS	4

extern uintptr_t	event_flags;
extern sig_atomic_t	event_timer_alarm;

extern uint32_t	*connection_counter_ptr;
extern uint32_t	*accept_mutex_ptr;
extern shmtx_t	accept_mutex;
extern uintptr_t	use_accept_mutex;
extern uintptr_t	accept_mutex_held;
extern uintptr_t	accept_events;
extern uintptr_t	accept_mutex_delay;
extern int		accept_disabled;

void event_accept(event_t *ev);
void process_events_adn_timers(cycle_t *cycle);
int handle_read_event(event_t *rev, int flags);
int hadnle_write_event(event_t *wev, size_t n);


#include "event_posted.h"


#endif


